package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Food;
import com.woniuxy.oldmanapi.service.FoodService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.LifeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class FoodController {
    @Autowired
    FoodService foodService;

    @GetMapping("/food/list")
    public ResponseResult<PageInfo<Food>> initSelectAllByCons(String name, int now, int size) {
        ResponseResult<PageInfo<Food>> result = new ResponseResult<>();
        try {
            PageInfo<Food> pageInfo = foodService.initSelectAllByCons(name, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @GetMapping("/food/selectAll")
    public ResponseResult<PageInfo<Food>> selectAll(String name, int now, int size) {
        ResponseResult<PageInfo<Food>> result = new ResponseResult<>();
        try {
            PageInfo<Food> pageInfo = foodService.selectAll(name, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @PostMapping("/food/addFood")
    public ResponseResult<Void> addFood(@RequestBody Food food) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            foodService.addFood(food);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PutMapping("/food/modifyFood")
    public ResponseResult<Void> modifyFood(@RequestBody Food food) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            foodService.modifyFood(food);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PutMapping("/food/modifyFoodStatus/{id}/{status}")
    public ResponseResult<Void> modifyFoodStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            foodService.modifyFoodStatus(id, status);
            if (status == 1) {
                result.setCode(200);
                result.setMsg("上架成功");
            } else {
                result.setCode(200);
                result.setMsg("下架成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @DeleteMapping("/food/deleteFood/{id}")
    public ResponseResult<Void> deleteFood(@PathVariable("id") Integer id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            foodService.deleteFoodById(id);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @GetMapping("/food/selectFoodById/{id}")
    public ResponseResult<Food> selectFoodById(@PathVariable("id") Integer id) {
        ResponseResult<Food> result = new ResponseResult<>();
        try {
            Food food = foodService.selectFoodById(id);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(food);
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
}
